/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/1/5 15:43
 *  三个线程交替打印ABC
 *      改变count的值可以实现count-1个线程的交替执行
 */
public class ThreadDemo22 {
    static volatile int count =0 ;
    static Object LOCKER = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (true){
                synchronized (LOCKER){
                    if (count%3==0){
                        System.out.println("A");
                        count++;
                        LOCKER.notifyAll();
                    }
                    //唤醒之后，不满足条件，继续等待
                    try {
                        LOCKER.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread t2 = new Thread(()->{
            while (true){
                synchronized (LOCKER){
                    if (count%3==1){
                        System.out.println("B");
                        count++;
                        LOCKER.notifyAll();
                    }
                    try {
                        LOCKER.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread t3 = new Thread(()->{
            while (true){
                synchronized (LOCKER){
                    if (count%3==2){
                        System.out.println("C");
                        count++;
                        LOCKER.notifyAll();
                    }
                    try {
                        LOCKER.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
//        Thread t4 = new Thread(()->{
//            while (true){
//                synchronized (LOCKER){
//                    while (count%5==3){
//                        System.out.println("D");
//                        count++;
//                        LOCKER.notifyAll();
//                    }
//                    try {
//                        LOCKER.wait();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//        Thread t5 = new Thread(()->{
//            while (true){
//                synchronized (LOCKER){
//                    while (count%5==4){
//                        System.out.println("E");
//                        count++;
//                        LOCKER.notifyAll();
//                    }
//                    try {
//                        LOCKER.wait();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
        t1.start();
        t2.start();
        t3.start();
//        t4.start();
//        t5.start();
    }
}
